<template>
	<view class="main">
		<view class="about" v-if="is_blogger == 1">
			<view class="toptab">
				<u-tabs name="cate_name" count="cate_count" :list="list" :is-scroll="false" :current="current"
					bar-width="80" :bar-style="barstyle" inactive-color="#555555" active-color="#000000"
					@change="changetoptab">
				</u-tabs>
			</view>
			<view class="top1">
				<view class="top">
					<view class="top_left">
						<view class="yue">
							账户余额
						</view>
						<view class="yuan">
							<text class="text">{{Userdata.balance?Userdata.balance:'0.00'}}</text>元
						</view>
						<view class="allyuan" v-if="er == true">
							总收益:{{Userdata.income?Userdata.income:'0.00'}}元
						</view>
						<view class="allyuan" v-if="er == false">
							总收益:{{Userdata.commission?Userdata.commission:'0.00'}}元
						</view>
					</view>
					<view class="top_right">
						<view class="tixian" @click="gotixian">
							提现
						</view>
					</view>
				</view>
			</view>
			<view class="center" v-if="current == 0">
				<view class="title" v-if="activeIndex==0">
					今日实时收益：{{allshouyi?allshouyi:'0'}}元
				</view>
				<view class="title" v-if="activeIndex==1">
					历史收益
				</view>
				<view class="title" v-if="activeIndex==2">
					提现记录
				</view>
				<view class="tabs">
					<view class="tabbox" v-for="(v,k) in title" :key="k" :class="{ tabbox1: k === activeIndex }"
						@click="handleTabClick(k)">
						{{v}}
					</view>
				</view>
			</view>
			<view class="datedate" v-if="activeIndex !=0 && current ==0">
				<view class="date">
					<text>当前日期</text>
					<view class="date1">
						<view class="date1_left">
							<u-icon name="calendar-fill" color="#FFA8A8" size="28"></u-icon>
						</view>
						<view class="date1_center">
							{{activeIndex==1?time1:time2}}
							<!-- {{time1}} -->
						</view>
						<view class="date1_right" @click="changedate">
							请选择日期
						</view>
					</view>
					<u-calendar v-model="showdate" :mode="mode" @change="change"></u-calendar>
				</view>
			</view>
			<view class="datedate" v-if="current ==1">
				<view class="date" v-if="is_team == 1">
					<text>当前日期</text>
					<view class="date1">
						<view class="date1_left">
							<u-icon name="calendar-fill" color="#FFA8A8" size="28"></u-icon>
						</view>
						<view class="date1_center">
							{{time3}}
						</view>
						<view class="date1_right" @click="changedate">
							请选择日期
						</view>
					</view>
					<u-calendar v-model="showdate" :mode="mode" @change="change"></u-calendar>
				</view>
			</view>
			<!-- 个人收益 -->
			<view class="bottom" v-if="current == 0">
				<Todayearnings v-if="activeIndex == 0" :TodayearningsList="TodayearningsList" @aaa='aaa'>
				</Todayearnings>
				<Historyearnings v-if="activeIndex == 1" :HistoryearningsList="HistoryearningsList" @aaa='aaa'>
				</Historyearnings>
				<Tixianjilu v-if="activeIndex == 2" :TixianjiluList="TixianjiluList" @aaa='aaa'></Tixianjilu>
			</view>
			<!-- 团队收益 -->
			<view class="bottom" v-if="current == 1">
				<Teamearnings @aaa='aaa' @bbb="bbb" :level='level' :Teamearningslist="Teamearningslist"
					v-if="is_team ==1">
				</Teamearnings>
				<view class="kong" v-if="is_team == 2">
					<image src="@/static/kong.svg" mode=""></image>
					<view class="kongtitle">
						您暂时还没有团队哦，赶紧去邀请用户填写您的邀请码，创建团队吧！
					</view>
				</view>
			</view>
			<MyPopup v-if="show" :content='content' @showshow='showshow'></MyPopup>
		</view>
		<view class="about2" v-if="is_blogger == 2">
			<view class="content">
				<view class="title">
					如何获得收益
				</view>
				<view class="a">
					1 .在书籍详情页/阅读页“分享赚钱”获取口令。
				</view>
				<view class="a">
					2 .在抖音视频分享该口令，邀请好友复制口令
				</view>
				<view class="a">
					3 .好友在抖音小程序“{{app_name}}”中粘贴口令，阅读书籍并完成充值后，你将获得相应收益
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import Todayearnings from "@/components/Todayearnings.vue"
	import Tixianjilu from "@/components/Tixianjilu.vue"
	import Historyearnings from "@/components/Historyearnings.vue"
	import Teamearnings from "@/components/Teamearnings.vue"
	import MyPopup from "@/components/Popup.vue"
	import {
		Earnings
	} from "@/request/api.js"
	import {
		app_name
	} from "@/request/open_from.js"
	export default {
		data() {
			return {
				current: 0,
				barstyle: {
					background: "#FF8383"
				},
				list: [{
					cate_name: '个人收益'
				}, {
					cate_name: '团队收益'
				}],
				title: ['今日收益', '历史收益', '提现记录'],
				activeIndex: 0,
				showdate: false,
				er: true,
				time1: '',
				time2: '',
				time3: '',
				is_team: '',
				mode: 'date',
				TodayearningsList: [],
				HistoryearningsList: [],
				TixianjiluList: [],
				Teamearningslist: [],
				level: '',
				Userdata: [], //账户余额
				allshouyi: '',
				show: false,
				content: '仅支持10元以上提现',
				is_blogger: '2', //是否为博主
				Tpage: 1,
				page_size: 15,
				TtotalPage: 0,
				TnewtotalPage: '',
				Hpage: 1,
				HtotalPage: 0,
				HnewtotalPage: '',
				TXpage: 1,
				TXtotalPage: 0,
				TXnewtotalPage: '',
				Teampage: 1,
				TeamtotalPage: 0,
				TeamnewtotalPage: '',
				app_name: app_name
			};
		},
		onLoad(option) {
			if (!option) {
				this.activeIndex = 2
				this.UserBalance()
				this.Tixianjilu()
			} else {
				this.is_blogger = option.is_blogger || 1
				if (option.activeIndex == 2) {
					this.activeIndex = 2
				}
				this.newdate1()
				this.UserBalance()
				this.Todayearnings()
				this.Historyearnings()
				this.Tixianjilu()
				this.Teamearnings()
			}

		},
		onShow() {
			this.Tpage = 1
			this.Hpage = 1
			this.TXpage = 1
			if (uni.getStorageSync('abc') == 2) {
				this.activeIndex = 2
				uni.removeStorageSync('abc');
				this.UserBalance()
				this.Tixianjilu()
			}
		},
		onHide() {
			this.Tpage = 1
			this.Hpage = 1
			this.TXpage = 1
			this.activeIndex = 0
			setTimeout(() => {
				uni.removeStorageSync('oldscrollTop');
			}, 500)
		},
		onUnload() {
			this.Tpage = 1
			this.Hpage = 1
			this.TXpage = 1
			this.activeIndex = 0
			setTimeout(() => {
				uni.removeStorageSync('oldscrollTop');
			}, 500)
		},
		methods: {
			handleTabClick(k) {
				if (this.activeIndex != k) {
					this.activeIndex = k
					if (k == 0) {
						this.Todayearnings()
					} else if (k == 1) {
						this.Historyearnings()
					} else if (k == 2) {
						this.Tixianjilu()
					}
				}
			},
			showshow(v) {
				this.show = v
			},
			aaa(v) {
				if (v == 1) {
					this.moreTodayearnings()
				} else if (v == 2) {
					this.moreHistoryearnings()
				} else if (v == 3) {
					this.moreTixianjilu()
				} else if (v == 4) {
					this.moreTeamearnings()
				}
			},
			bbb(v) {
				for (let i = 0; i < this.Teamearningslist.length; i++) {
					if (i === v) {
						this.Teamearningslist[i].check = !this.Teamearningslist[i].check
					} else {
						this.Teamearningslist[i].check = false
					}
				}
			},
			newdate1() {
				const currentDate = new Date();
				const year = currentDate.getFullYear();
				const month = ('0' + (currentDate.getMonth() + 1)).slice(-2);
				const day = ('0' + currentDate.getDate()).slice(-2);
				const formattedDate = `${year}-${month}-${day}`;
				// const today = new Date().toLocaleDateString()
				this.time1 = formattedDate
				this.time2 = formattedDate
				this.time3 = formattedDate
			},
			changedate() {
				this.showdate = true
			},
			changetoptab(index) {
				this.current = index;
				if (index == 1) {
					this.er = false
				} else {
					this.er = true
				}
			},
			change(e) {
				if (this.current == 0) {
					if (this.activeIndex == 1) {
						this.time1 = e.result
						this.Hpage = 1
						this.Historyearnings()
					} else if (this.activeIndex == 2) {
						this.time2 = e.result
						this.TXpage = 1
						this.Tixianjilu()
					}
				} else {
					this.time3 = e.result
					this.Teampage = 1
					this.Teamearnings()
				}

			},
			// 账户余额
			async UserBalance() {
				let res = await Earnings.UserBalance()
				this.Userdata = res.data
				this.is_team = res.data.is_team
			},
			// 今日收益
			async Todayearnings() {
				let res = await Earnings.Today(1)
				this.TnewtotalPage = Math.ceil((res.data.page_info.total) / this.page_size)
				this.allshouyi = res.data.page_info.today_total
				this.TodayearningsList = res.data.data
				this.TtotalPage = res.data.page_info.total
			},
			// 历史收益
			async Historyearnings() {
				let res = await Earnings.History(this.time1, 1)
				this.HnewtotalPage = Math.ceil((res.data.page_info.total) / this.page_size)
				this.HistoryearningsList = res.data.data
				this.HtotalPage = res.data.page_info.total

			},
			// 提现记录
			async Tixianjilu() {
				let res = await Earnings.TiXian(this.time2, 1)
				this.TXnewtotalPage = Math.ceil((res.data.page_info.total) / this.page_size)
				this.TixianjiluList = res.data.data
				this.TXtotalPage = res.data.page_info.total
			},
			//团队收益
			async Teamearnings() {
				let res = await Earnings.Team(this.time3, 1)
				if (res.data.page_info.level == 2) {
					this.Teamearningslist = res.data.data
				} else if (res.data.page_info.level == 3) {
					this.Teamearningslist = res.data.data.map(item => {
						item.check = false
						return item
					})
				}
				this.newtotalPage = Math.ceil((res.data.page_info.total) / this.page_size)
				this.TeamtotalPage = res.data.page_info.total
				this.level = res.data.page_info.level
			},
			gotixian() {
				if (this.Userdata.is_withdrawal == 1) {
					let money = this.Userdata.balance
					let tian = this.Userdata.is_withdrawal_info
					let withdrawalInfo = encodeURIComponent(JSON.stringify(this.Userdata.withdrawalInfo))
					if (this.Userdata.balance >= 10) {
						if (tian == 1) {
							uni.navigateTo({
								url: `/pages/Tixian/Tixian?money=${money}&withdrawalInfo=${withdrawalInfo}&tian=${tian}`,
							})
						} else {
							uni.navigateTo({
								url: `/pages/Tixian/Tixian?money=${money}`
							})
						}

					} else {
						this.show = true
					}
				} else {
					this.content = "该账号暂不支持提现"
					this.show = true
				}

			},
			//下滑加载更多今日收益
			async moreTodayearnings() {
				this.Tpage++
				if (this.Tpage > this.TnewtotalPage) return
				let res = await Earnings.Today(this.Tpage)
				let pageList = res.data.data
				this.TodayearningsList = this.TodayearningsList.concat(pageList)
			},
			//下滑加载更多历史收益
			async moreHistoryearnings() {
				this.Hpage++
				if (this.Hpage > this.HnewtotalPage) return
				let res = await Earnings.History(this.time1, this.Hpage)
				let pageList = res.data.data
				this.HistoryearningsList = this.HistoryearningsList.concat(pageList)
			},
			//下滑加载更多提现记录
			async moreTixianjilu() {
				this.TXpage++
				if (this.TXpage > this.TXnewtotalPage) return
				let res = await Earnings.TiXian(this.time2, this.TXpage)
				let pageList = res.data.data
				this.TixianjiluList = this.TixianjiluList.concat(pageList)
			},
			//下滑加载更多团队收益
			async moreTeamearnings() {
				this.Teampage++
				if (this.Teampage > this.newtotalPage) return
				let res = await Earnings.Team(this.time3, this.Teampage)
				let pageList = []
				if (res.data.page_info.level == 2) {
					pageList = res.data.data
				} else if (res.data.page_info.level == 3) {
					pageList = res.data.data.map(item => {
						item.check = false
						return item
					})
				}
				this.Teamearningslist = this.Teamearningslist.concat(pageList)
			},
		},
		components: {
			Todayearnings,
			Tixianjilu,
			Historyearnings,
			MyPopup,
			Teamearnings
		}
	}
</script>

<style lang="less" scoped>
	@import url("./Earnings.less");
</style>